package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ImageService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private ImageService imageService;


    /**
     * 添加套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @RequestMapping("/add")
    public Result add(String[] checkgroupIds, @RequestBody Setmeal setmeal){
        setmealService.add(checkgroupIds,setmeal);

        // 已经成功添加到数据库中图片地址保存到redis缓存中
        imageService.saveUsedImage(setmeal.getImg());
        return new Result(true,"新增套餐成功");
    }

    /**
     * 分页条件查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setmealService.findPage(queryPageBean);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(String id){
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,"查询成功",setmeal);
    }

    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/edit")
    public Result edit(String[] checkGroupIds, @RequestBody Setmeal setmeal){
        setmealService.edit(checkGroupIds,setmeal);

        // 已成功修改数据库后的图片地址保存到redis缓存中
        imageService.saveUsedImage(setmeal.getImg());
        return new Result(true,"编辑套餐成功");
    }


}
